<?php

/**
 * 三方接口 配置数据
 * @author HelloWorld
 * 2019-08-14
 * UTF-8
 */
class Lib_Var
{

    // 引调案件结构
    public static $excel_title = [
        'caseDetail'=>[
            'contractNo' => [
                'site' => 0,
                'mast' => 1,
                'name' => '合同编号（必填）',
            ],
            'batshId' => [
                'site' => 1,
                'mast' => 1,
                'name' => '案件标识号（可方便案件分配，案件调解必填）',
            ],
            'litigantName' => [
                'site' => 2,
                'mast' => 1,
                'name' => '当事人姓名（若有公债人|间隔）',
            ],
            'cardId' => [
                'site' => 3,
                'mast' => 1,
                'name' => '证件号码（若有公债人|间隔）',
                'check' => 'checkCard',
            ],
            'telephone' => [
                'site' => 4,
                'mast' => 1,
                'array' => 1,
                'name' => '手机号码（多个号码用,间隔，若有公债人|间隔）',
                'check' => 'checkMobile',
            ],
            'loanPlatform' => [
                'site' => 5,
                'mast' => 1,
                'name' => '借款平台（必填）',
            ],
            'loanAmount' => [
                'site' => 6,
                'mast' => 1,
                'name' => '借款金额（必填）',
                'check' => 'checkAmount',
            ],
            'loanTerm' => [
                'site' => 7,
                'mast' => 1,
                'name' => '借款期限（必填）',
                'check' => 'checkInt',
            ],
            'debtAmount' => [
                'site' => 8,
                'mast' => 1,
                'name' => '欠还本金（必填）',
                'check' => 'checkAmount',
            ],
            'debtInte' => [
                'site' => 9,
                'mast' => 0,
                'name' => '欠还利息（含罚息）',
                'check' => 'checkAmount',
            ],
            'debtTotal' => [
                'site' => 10,
                'mast' => 1,
                'name' => '欠款总额（必填）',
                'check' => 'checkAmount',
            ],
            'claimAmount' => [
                'site' => 11,
                'mast' => 1,
                'name' => '诉求金额（必填）',
                'check' => 'checkAmount',
            ],
            'overdueDate' => [
                'site' => 12,
                'mast' => 0,
                'name' => '逾期日期',
                'check' => 'checkDate',
            ],
            'putoutDate' => [
                'site' => 13,
                'mast' => 0,
                'name' => '放款日期',
                'check' => 'checkDate',
            ],
            'debtTargetDate' => [
                'site' => 14,
                'mast' => 0,
                'name' => '欠款暂记日',
                'check' => 'checkDate',
            ],
            'combinRate' => [
                'site' => 15,
                'mast' => 0,
                'name' => '综合利率',
                'check' => 'checkRate',
            ],
            'entrustExpireTime' => [
                'site' => 16,
                'mast' => 0,
                'name' => '委托到期时间(默认分配时间+30天)',
                'check' => 'checkDate',
            ],
            'mediatorName' => [
                'site' => 17,
                'mast' => 0,
                'name' => '调解员(根据姓名分配案件，跳过分配步骤)',
            ],
            'mediationNum'=>[
                'site' => 18,
                'mast' => 0,
                'name' => '调解案号(若上传了案号，则不重新生成)',
            ]

        ],
        'publicCase'=>[
            'litigantName' => [
                'site' => 0,
                'mast' => 1,
                'name' => '当事人姓名（必填）',
            ],
            'cardId' => [
                'site' => 1,
                'mast' => 1,
                'name' => '证件号码（后六位/全号,必填）',
                'check' => 'checkCard6',
            ],
            'assetsName' => [
                'site' => 2,
                'mast' => 1,
                'name' => '资产方（原告,必填）',
            ],
            'debtPrincipal' => [
                'site' => 3,
                'mast' => 1,
                'name' => '欠款本金（必填）',
                'check' => 'checkAmount',
            ],
            'debtTarget' => [
                'site' => 4,
                'mast' => 1,
                'name' => '标的金额（必填）',
                'check' => 'checkAmount',
            ],
            'mediationNum'=>[
                'site' => 5,
                'mast' => 0,
                'name' => '调解案号(若上传了案号，则不重新生成)',
            ]

        ],

        'debtor' => [
            'litigantName' => [
                'mast' => 1,
                'name' => '姓名',
            ],
            'cardId' => [
                'mast' => 1,
                'name' => '证件号码',
                'check' => 'checkCard',
            ],
            'telephone' => [
                'mast' => 1,
                'array' => 1,
                'name' => '手机号码',
                'check' => 'checkMobile',
            ],
        ]
    ];

    public static function checkData($fields,$data){
        $error_desc = '';
        $error = 0;
        if(empty($fields) || empty($data)){
           return ['error'=>1,'error_desc'=>'系统参数异常'];
        }
        foreach ($fields as $key => $field) {
            $site = $field['site'] + 1;
            $data[$key] = trim(Func_Common::convertToUtf8($data[$key])," \t\n\r\0\x0B");
            //不为空判断
            if ($field['mast']) {
                if (empty($data[$key])) {
                    $error = 1;
                    $error_desc .= $site.'列 '.$field['name'].':为空 ';
                }
            }

            //验证格式
            if (!empty($data[$key]) && isset($field['check']) && (empty($field['array']) || $field['array'] != 1)) {
                $function = $field['check'];
                if (!Func_Common::$function(trim($data[$key]))) {
                    $error = 1;
                    $error_desc .= $site.'列 '.$field['name'].':格式错误 ';
                }
            }

            //多数组隔开
            if (!empty($data[$key]) && isset($field['array']) && $field['array'] == 1) {

                $data[$key] = str_replace('，',',',$data[$key]);
                $temp =  explode(',', $data[$key]);
                $arr = [];
                if (isset($field['check']) ) {
                    $function = $field['check'];
                    foreach ($temp as $val){
                        if (!Func_Common::$function($val)) {
                            continue;
                        }
                        if($function == 'checkAmount'){
                            $val = number_format($val, 2, '.', '');
                        }
                        $arr[] = $val;
                    }
                }
                $data[$key] = $arr;
                if($field['mast'] && empty($data[$key])){
                    $error = 1;
                    $error_desc .= $site.'列 '.$field['name'].':格式错误 ';
                }
            }
        }
        return ['error'=>$error,'error_desc'=>$error_desc];

    }


}